﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class Reapers : Form
    {
        public Reapers()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;
        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        #region Form events
        private void Reapers_Load(object sender, EventArgs e)
        {
            LoadReapers();
        } 
        #endregion

        #region ListView events
        private void lvwReapers_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonEdit.Enabled = lvwReapers.SelectedItems.Count == 1;
            toolStripButtonRemove.Enabled = lvwReapers.SelectedItems.Count > 0;
            editToolStripMenuItem.Enabled = lvwReapers.SelectedItems.Count == 1;
            deleteToolStripMenuItem.Enabled = lvwReapers.SelectedItems.Count > 0;
        }
        private void lvwReapers_Resize(object sender, EventArgs e)
        {
            columnHeaderServer.Width = lvwReapers.ClientSize.Width;
        }
        private void lvwReapers_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonEdit_Click(sender, e);
            }
        }
        #endregion

        #region Toolbar and context menu events
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            ReaperEdit reaperEdit = new ReaperEdit();
            if (reaperEdit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                eventScavengerDAL.InsertReaper(reaperEdit.SelectedReaper);
                LoadReapers();
            }
        }
        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (lvwReapers.SelectedItems.Count == 1)
            {
                ReaperEdit reaperEdit = new ReaperEdit();
                reaperEdit.SelectedReaper = (Reaper)lvwReapers.SelectedItems[0].Tag;
                if (reaperEdit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    eventScavengerDAL.UpdateReaper(reaperEdit.SelectedReaper);
                    lvwReapers.SelectedItems[0].Tag = reaperEdit.SelectedReaper;
                    lvwReapers.SelectedItems[0].Text = reaperEdit.SelectedReaper.ServerName;
                }
            }
        }
        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwReapers.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Are you sure you want to delete the selected reaper(s)?\r\nIf there are any collectors still linked with it the delete will fail", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvwReapers.SelectedItems)
                        {
                            Reaper reaper = (Reaper)lvi.Tag;
                            eventScavengerDAL.DeleteReaper(reaper);
                        }
                    }
                    LoadReapers();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadReapers();
        } 
        #endregion

        #region Private methods
        private void LoadReapers()
        {
            try
            {
                lvwReapers.Items.Clear();
                List<Reaper> reapers = eventScavengerDAL.GetReaperList();
                foreach (Reaper reaper in reapers)
                {
                    ListViewItem lvi = new ListViewItem(reaper.ServerName);
                    lvi.Tag = reaper;
                    lvwReapers.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

    }
}
